package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.ProductUnit;
import com.mvp.shq.washer.entity.shop.result.OrderDetailListRestult;
import com.mvp.shq.washer.utils.ImageUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/16.
 */

public class OrderProductCommentAdapter extends RecyclerView.Adapter<OrderProductCommentAdapter.ViewHolder> {


    private Context context;
    private List<OrderDetailListRestult> datas;
    private ImageUtil imageUtil;
    private OnCommentContentChangeListener listener;

    public OrderProductCommentAdapter(Context mContext, List<OrderDetailListRestult> list) {
        context = mContext;
        datas = list;
        imageUtil = new ImageUtil(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_comment, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvProductName.setText(datas.get(position).getProductName());
        imageUtil.loadUrlImage(datas.get(position).getProductImg(),holder.imgProductImg);
        holder.etCommentContent.setTag(position);
        holder.etCommentContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if ((int)holder.etCommentContent.getTag() == position) {//设置tag解决错乱问题
                    if (listener != null){
                        listener.onEdittextChange(position,s.toString());
                    }
                }
            }
        });
        holder.rbStart.setTag(position);
        holder.rbStart.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                if ((int)holder.rbStart.getTag() == position) {//设置tag解决错乱问题
                    if (listener != null){
                        listener.onRatingBarChange(position,rating);
                    }
                }
            }
        });
    }

    public void setListener(OnCommentContentChangeListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_product_img)
        ImageView imgProductImg;
        @BindView(R.id.tv_product_name)
        TextView tvProductName;
        @BindView(R.id.rb_start)
        RatingBar rbStart;
        @BindView(R.id.et_comment_content)
        EditText etCommentContent;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnCommentContentChangeListener{
        void onEdittextChange(int position,String content);
        void onRatingBarChange(int position,float rating);
    }
}
